package com.example.nutritionmanager.home

import android.content.Intent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.nutritionmanager.R
import com.example.nutritionmanager.record.MealType
import com.example.nutritionmanager.record.NutritionRecordActivity

@Composable
fun AddMealButtons(navController: NavController) {
    val context = LocalContext.current
    Text(
        text = "饮食日记",
        fontSize = 16.sp,
        fontWeight = FontWeight.Bold,
        modifier = Modifier.padding(bottom = 8.dp)
    )
    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        elevation = CardDefaults.cardElevation(4.dp),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface)
    ){
        // 添加餐食按钮
        Row(
            modifier = Modifier.fillMaxWidth().padding(vertical = 20.dp),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            MealButton("早餐", onClick = {
                // 创建启动 Intent 并启动 NutritionRecordActivity
                val intent = Intent(context, NutritionRecordActivity::class.java).apply {
                    putExtra("meal_type", MealType.MEAL_TYPE_BREAKFAST)
                }
                context.startActivity(intent)
            }, R.drawable.breakfast)
            MealButton("午餐", onClick = {
                val intent = Intent(context, NutritionRecordActivity::class.java).apply {
                    putExtra("meal_type", MealType.MEAL_TYPE_LUNCH)
                }
                context.startActivity(intent)
            }, R.drawable.lunch)
            MealButton("晚餐", onClick = {
                val intent = Intent(context, NutritionRecordActivity::class.java).apply {
                    putExtra("meal_type", MealType.MEAL_TYPE_DINNER)
                }
                context.startActivity(intent)
            }, R.drawable.dinner)
            MealButton("零食", onClick = {
                val intent = Intent(context, NutritionRecordActivity::class.java).apply {
                    putExtra("meal_type", MealType.MEAL_TYPE_SNACK)
                }
                context.startActivity(intent)
            }, R.drawable.snacks)
        }
    }
}


@Composable
fun MealButton(label: String, onClick: () -> Unit, resourceId: Int) {
    Column(
        modifier = Modifier
            .width(80.dp)
            .clickable(onClick = onClick),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center) {
        Image(
            painter = painterResource(resourceId),
            contentDescription = "餐食图标",
            modifier = Modifier.size(30.dp),
            colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.secondary)
        )
        Row(
            modifier = Modifier.padding(top = 4.dp),  // 增加上下间距
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painter = painterResource(R.drawable.add),
                contentDescription = "添加图标",
                modifier = Modifier.size(10.dp)
            )
            Text(text = label, color = MaterialTheme.colorScheme.onSurface, fontSize = 12.sp)
        }
    }
}